package com.molichuxing.services.delay;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

public class OrderDelay implements Delayed {
	private static final long serialVersionUID = 8001363688872591442L;

	/**
	 * 订单号
	 */
	private Long orderCode;
	/**
	 * 用户id
	 */
	private Integer userId;
	/**
	 * 订单过期时间
	 */
	private long timeout;

	public OrderDelay() {
	}

	/**
	 * 
	 * @param orderCode
	 *            订单id
	 * @param userId
	 *            用户id
	 * @param timeout
	 *            超时时间
	 */
	public OrderDelay(Long orderCode, Integer userId, long timeout) {
		this.orderCode = orderCode;
		this.userId = userId;
		this.timeout = timeout;
	}

	public Long getOrderCode() {
		return orderCode;
	}

	public void setOrderCode(Long orderCode) {
		this.orderCode = orderCode;
	}

	public Integer getUserId() {
		return userId;
	}

	public void setUserId(Integer userId) {
		this.userId = userId;
	}

	public long getTimeout() {
		return timeout;
	}

	public void setTimeout(long timeout) {
		this.timeout = timeout;
	}

	@Override
	public String toString() {
		return "OrderDelay [orderCode=" + orderCode + ", userId=" + userId + ", timeout=" + timeout + "]";
	}

	@Override
	public int compareTo(Delayed delayed) {
		if (delayed == this) {
			return 0;
		}
		long excessTime = getDelay(TimeUnit.NANOSECONDS) - delayed.getDelay(TimeUnit.NANOSECONDS);
		return excessTime == 0 ? 0 : (excessTime < 0 ? -1 : 1);
	}

	@Override
	public long getDelay(TimeUnit unit) {
		return unit.convert(this.timeout - System.currentTimeMillis(), TimeUnit.MILLISECONDS);
	}

}
